import Common from '../../common/common.js';
import BaseWin from '../base/BaseWin.js';
import FriendListChecked from './FriendListChecked.js';
import GroupMemberList from './GroupMemberList.js';
import TimNet from '../../timnet.js';


/***
 * 添加群组窗口
 ***/
function CreateGroupWin () {
    //人员列表对象
    this.userList = new FriendListChecked();

    BaseWin.call(this);
    this.element.className += " createGroupWin";
    //基础信息的表单
    this.form;

}
let prototype = Object.create(BaseWin.prototype);
prototype.constructor = CreateGroupWin;
CreateGroupWin.prototype  = prototype;

CreateGroupWin.prototype.initBox = function(){
    const form = Common.tool.createElement({
        label:"form",
        props:{name:"createGroupWinForm"},
        childNodes:[
            {
                label:"table",
                childNodes:[
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"群头像"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"avatar",maxLength:100}} ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"群组名称"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"name",maxLength:30}} ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"群简介"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"introduction",maxLength:240}} ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"群公告"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"notification",maxLength:300}} ]}
                        ]
                    }
                ]
            }
        ]
    });
    this.form = form;
    this.element.win.box.appendChild(form);
    this.element.win.box.appendChild(this.userList.getElement());

}
//提交信息
CreateGroupWin.prototype.submit = function(){
    let createGroupWinForm = document.forms["createGroupWinForm"];
    let formData = new FormData(createGroupWinForm);
    let name = formData.get("name");

    if(Common.tool.isNull(name)){
        alert("请输入群组名称");
        return;
    }
    let checkedUserIds = this.userList.getCheckedUserID();
    if(checkedUserIds.length == 0){
        alert("请选择组员");
        return;
    }

     TimNet.createGroup(
        formData.get("avatar"),
         name,
        formData.get("introduction"),
        formData.get("notification"),
        formData.get("joinOption"),
        checkedUserIds,
        ()=>{
             this.cancel();
         }
    )

}
//提交成功后执行
CreateGroupWin.prototype.submitSuccess = function(){}
CreateGroupWin.prototype.open =function(){
    this.create();
}

/***
 * 修改群组窗口--添加成员
 ***/
function AddGroupMemberWin () {
    //人员列表对象
    this.userList = new FriendListChecked();
    BaseWin.call(this);
    this.element.className += " createGroupWin AddGroupMemberWin";

}
let prototype3 = Object.create(BaseWin.prototype);
prototype3.constructor = AddGroupMemberWin;
AddGroupMemberWin.prototype  = prototype3;

AddGroupMemberWin.prototype.initBox = function(){
    this.element.win.box.appendChild(this.userList.getElement());
}


/***
 * 修改群组窗口
 ***/

function UpdateGroupWin (data) {
    //人员列表对象
    this.userList = new GroupMemberList(data.groupID);
    this.data = data;
    BaseWin.call(this);
    this.element.className += " createGroupWin updateGroupWinForm";
    //基础信息的表单
    this.form;
}
let prototype2 = Object.create(BaseWin.prototype);
prototype2.constructor = UpdateGroupWin;
UpdateGroupWin.prototype  = prototype2;

UpdateGroupWin.prototype.initBox = function(){
    TimNet.getGroupProfile(this.data.groupID,groupData=>{
        const form = Common.tool.createElement({
            label:"form",
            props:{name:"updateGroupWinForm"},
            childNodes:[
                {
                    label:"table",
                    childNodes:[
                        {
                            label:"tr",
                            childNodes:[
                                { label:"th", props:{innerText:"群头像"}  },
                                { label:"td", childNodes:[{label:"input",props:{type:"text",name:"avatar",maxLength:100,value:groupData.avatar}} ]}
                            ]
                        },
                        {
                            label:"tr",
                            childNodes:[
                                { label:"th", props:{innerText:"群组名称"}  },
                                { label:"td", childNodes:[{label:"input",props:{type:"text",name:"name",maxLength:30,value:groupData.name}} ]}
                            ]
                        },
                        {
                            label:"tr",
                            childNodes:[
                                { label:"th", props:{innerText:"群简介"}  },
                                { label:"td", childNodes:[{label:"input",props:{type:"text",name:"introduction",maxLength:240,value:groupData.introduction}} ]}
                            ]
                        },
                        {
                            label:"tr",
                            childNodes:[
                                { label:"th", props:{innerText:"群公告"}  },
                                { label:"td", childNodes:[{label:"input",props:{type:"text",name:"notification",maxLength:300,value:groupData.notification}} ]}
                            ]
                        }
                    ]
                }
            ]
        });
        this.form = form;
        this.element.win.box.appendChild(form);
        this.element.win.box.appendChild(this.userList.getElement());
    });
}

//提交信息
UpdateGroupWin.prototype.submit = function(){
    let updateGroupWinForm = document.forms["updateGroupWinForm"];
    let formData = new FormData(updateGroupWinForm);
    let name = formData.get("name");
    if(Common.tool.isNull(name)){
        alert("请输入群组名称");
        return;
    }

    let items = this.userList.getItems();
    let deleteItems = items.filter(item=>item.isDelete).map(item=>item.data.userID);
    //移除
    if(deleteItems.length > 0){
         TimNet.deleteGroupMember(this.data.groupID,deleteItems);
    }
    //添加
    let addItems = items.filter(item=>!item.role).map(item=>item.data.userID);
    if(addItems.length > 0){
        TimNet.addGroupMember(this.data.groupID,addItems,imResponse=>{
            let failureUserIDList = imResponse.data.failureUserIDList;
            if(failureUserIDList.length > 0){
                alert(failureUserIDList.json("/")+"添加失败");
            }
          });
    }

    //修改基本信息
    TimNet.updateGroupProfile(this.data.groupID,formData.get("avatar"),
        name,
        formData.get("introduction"),
        formData.get("notification"), ()=>{
          this.cancel();
        })


}

//提交成功后执行
UpdateGroupWin.prototype.submitSuccess = function(){}
UpdateGroupWin.prototype.open =function(){
    this.create();
}


export {CreateGroupWin,UpdateGroupWin,AddGroupMemberWin} ;
